package pers.qianyu.module.logging.service.impl;

import cn.hutool.log.*;
import com.alibaba.excel.*;
import com.alibaba.excel.write.metadata.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.scheduling.annotation.*;
import org.springframework.stereotype.*;
import pers.qianyu.module.core.domain.logging.converter.*;
import pers.qianyu.module.core.domain.logging.dto.*;
import pers.qianyu.module.core.domain.logging.entity.*;
import pers.qianyu.module.core.domain.logging.query.*;
import pers.qianyu.module.core.util.*;
import pers.qianyu.module.logging.dao.*;
import pers.qianyu.module.logging.service.*;

import java.io.*;
import java.util.*;

/**
 * Excel 数据导出服务实现类
 *
 * @author mizzle rain
 * @date 2020年11月2日20:41:27
 */
@Service("excelExportService")
public class ExcelExportServiceImpl implements ExcelExportService {
    @Autowired
    private RequestLogDao requestLogDao;
    @Autowired
    private FileService fileService;
    @Autowired
    private RequestLogConverter requestLogConverter;

    /**
     * 执行数据库查询
     *
     * @param out   查询结果输出流
     * @param query 查询条件
     */
    private void query(OutputStream out, RequestLogQuery query) {
        ExcelWriter writer = EasyExcelFactory.write(out, RequestLogExportDTO.class).build();

        Page<RequestLogPO> page = new Page<>(query.getPage(), query.getLimit());
        RequestLogPO requestLogPO = requestLogConverter.convertToPO(query);
        QueryWrapper<RequestLogPO> wrapper = new QueryWrapper<>(requestLogPO);
        String startTimeStr = query.getStartTime();
        String endTimeStr = query.getEndTime();
        if (Objects.nonNull(startTimeStr)) {
            long startTime = DateTimeUtil.toMillis(startTimeStr);
            long endTime = Objects.isNull(endTimeStr) ? System.currentTimeMillis() :
                    DateTimeUtil.toMillis(endTimeStr);
            wrapper.between("create_time", startTime, endTime);
        }
        Page<RequestLogPO> requestLogPOPage = requestLogDao.selectPage(page, wrapper);

        List<RequestLogExportDTO> requestLogExportDTOList = requestLogConverter
                .convertToRequestLogExportDTOList(requestLogPOPage.getRecords());
        WriteSheet writeSheet = EasyExcelFactory.writerSheet("日志信息").build();
        writer.write(requestLogExportDTOList, writeSheet);
        writer.finish();
        LogFactory.get().info("导出数据完成");
    }

    @Override
    public void export(RequestLogQuery requestLogQuery, String fileName) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        // 1. 实现数据导出到 Excel 中
        query(out, requestLogQuery);
        // 2. 实现文件上传
        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
        fileService.upload(in, fileName);
    }

    @Async("exportServiceExecutor")
    @Override
    public void asyncExport(RequestLogQuery requestLogQuery, String fileName) {
        export(requestLogQuery, fileName);
    }
}
